﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dack_Web2.BUS;
using Dack_Web2.DTO;
using System.Data.OleDb;
namespace Dack_Web2
{
    public partial class CongTy_DangDeal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentContext.IsLogged)
                {
                    if (CongTyBUS.GetCongTy(CurrentContext.UserName) != null)
                    {
                        User Temp = Dack_Web2.User.FromId(CurrentContext.UserName);
                        string sql = string.Format("select * from [SanPham] sp, [LoaiSanPham] Lsp where sp.MaLoaiSanPham = Lsp.MaLoaiSanPham and MaUser={0} and MaSanPham not in (select MaSanPham from Deal where NgayKetThuc <= @NgayHienTai)", Temp.MaUser);
                        OleDbCommand cmd = new OleDbCommand(sql);
                        OleDbParameter[] pr = { new OleDbParameter("@NgayHienTai", DateTime.Now.Date) };
                        GridView1.DataSource = Sql_DataProvider.SQL_ExecuteTable(cmd, pr);
                        GridView1.DataBind();
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }

                drThang.Items.Clear();
                drThang0.Items.Clear();
                for (int i = 1; i < 13; i++)
                {
                    drThang.Items.Add(i.ToString());
                    drThang0.Items.Add(i.ToString());
                }
                drThang.SelectedIndex = 1;
                drThang0.SelectedIndex = 1;

                drNam.Items.Clear();
                drNam0.Items.Clear();
                for (int i = 1900; i < 2800; i++)
                {
                    drNam.Items.Add(i.ToString());
                    drNam0.Items.Add(i.ToString());
                }
                drNam.SelectedIndex = 113;
                drNam0.SelectedIndex = 113;
                drThang.SelectedIndex = DateTime.Now.Month-1;
                drThang_SelectedIndexChanged(null, null);
                drNgay.SelectedIndex = DateTime.Now.Day -1;
                drThang0_SelectedIndexChanged(null, null);
            }
        }

        protected void btDangDeal_Click(object sender, EventArgs e)
        {
            if (CurrentContext.IsLogged)
            {
                if (CongTyBUS.GetCongTy(CurrentContext.UserName) != null)
                {
                    DealDTO deal = new DealDTO();
                    DealBUS bus = new DealBUS();
                    deal.MaSanPham = int.Parse(txtMaSanPham.Text);
                    deal.Giam = int.Parse(txtGiam.Text);
                    DateTime batdau = new DateTime(int.Parse(drNam.SelectedItem.ToString()),
                                            int.Parse(drThang.SelectedItem.ToString()),
                                            int.Parse(drNgay.SelectedItem.ToString()));
                    DateTime ketthuc = new DateTime(int.Parse(drNam0.SelectedItem.ToString()),
                                            int.Parse(drThang0.SelectedItem.ToString()),
                                            int.Parse(drNgay0.SelectedItem.ToString()));
                    deal.NgayBatDau = batdau;
                    deal.NgayKetThuc = ketthuc;
                    deal.SoLuong = int.Parse(txtSoLuong.Text);
                    deal.XacNhan = false;
                    bus.Insert(deal);
                    lbThongBao.Text = "Bạn đã đăng deal thành công, có thể đăng deal khác ...";
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx");
            }
        }

        protected void drThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drThang.SelectedItem.Text == "2")
            {
                drNgay.Items.Clear();
                for (int i = 1; i < 30; i++)
                {
                    drNgay.Items.Add(i.ToString());
                }
            }
            else
            {
                drNgay.Items.Clear();
                for (int i = 1; i < 32; i++)
                {
                    drNgay.Items.Add(i.ToString());
                }
            }
        }

        protected void drThang0_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drThang0.SelectedItem.Text == "2")
            {
                drNgay0.Items.Clear();
                for (int i = 1; i < 30; i++)
                {
                    drNgay0.Items.Add(i.ToString());
                }
            }
            else
            {
                drNgay0.Items.Clear();
                for (int i = 1; i < 32; i++)
                {
                    drNgay0.Items.Add(i.ToString());
                }
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtMaSanPham.Text = GridView1.SelectedRow.Cells[0].Text;
            txtTenSanPham.Text = GridView1.SelectedRow.Cells[2].Text;
        }
    }
}